/**
 * Copyright 2018 asiainfo Inc.
 **/
package com.myz.service.hystrix.web;

import com.myz.service.hystrix.service.IHelloServcie;
import com.myz.service.hystrix.service.IHystrixRedisCacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * @author maoyz on 2018/9/7
 * @version: v1.0
 */
@RequestMapping("/hystrix")
@RestController
public class HelloController {

    @Autowired
    private IHelloServcie helloServcie;

    @Autowired
    private IHystrixRedisCacheService hystrixRedisCacheService;

    @RequestMapping("/hello")
    public String hello() {
        return helloServcie.index();
    }

    @RequestMapping("/hello0")
    public String hello0() {
        return helloServcie.indexSemaphore();
    }

    @RequestMapping("/show")
    public String show() {
        return helloServcie.showThread();
    }

    @RequestMapping("/batch")
    public String batch() throws ExecutionException, InterruptedException {
        Future<String> batch1 = helloServcie.batch(1);
        Future<String> batch2 = helloServcie.batch(2);
        Future<String> batch3 = helloServcie.batch(3);

        return batch1.get() + " -> " + batch2.get() + " -> " + batch3.get();
    }

    @RequestMapping("/circuit")
    public String circuit(@RequestParam("id") Integer id) {
        return helloServcie.circuit(id);
    }

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    public String get(@PathVariable(value = "id") Integer id) {
        return hystrixRedisCacheService.cacheGet(id);
    }

    @RequestMapping(value = "/get/{id}", method = RequestMethod.DELETE)
    public String del(@PathVariable(value = "id") Integer id) {
        return hystrixRedisCacheService.cacheDel(id);
    }

}
